/*
 * $Id: WorkerSelectFrame.java 14 2008-12-04 13:13:33Z rdubisz $
 */
package wdp.worker;

import java.awt.EventQueue;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.jdesktop.application.Action;
import wdp.WdpApp;
import wdp.entities.ready.Worker;

public class WorkerSelectFrame extends JDialog {

    public WorkerSelectFrame(JFrame parent) {
        super(parent, true);
        initComponents();
        TableSelectionListener listener = new TableSelectionListener();
        masterTable.getSelectionModel().addListSelectionListener(listener);
        entityManager.getTransaction().begin();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

    entityManager = javax.persistence.Persistence.createEntityManagerFactory("db.fdbPU").createEntityManager();
    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wdp.WdpApp.class).getContext().getResourceMap(WorkerSelectFrame.class);
    query = entityManager.createQuery(resourceMap.getString("query.query")); // NOI18N
    list = org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
    masterScrollPane = new javax.swing.JScrollPane();
    masterTable = new javax.swing.JTable();
    newButton = new javax.swing.JButton();
    jButtonSelect = new javax.swing.JButton();
    jButtonNone = new javax.swing.JButton();

    FormListener formListener = new FormListener();

    setName("Form"); // NOI18N

    masterScrollPane.setName("masterScrollPane"); // NOI18N

    masterTable.setName("masterTable"); // NOI18N

    org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list, masterTable);
    org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${firstname}"));
    columnBinding.setColumnName("Firstname");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${surname}"));
    columnBinding.setColumnName("Surname");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idPost}"));
    columnBinding.setColumnName("Id Post");
    columnBinding.setColumnClass(wdp.entities.ready.Post.class);
    columnBinding.setEditable(false);
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idTeam}"));
    columnBinding.setColumnName("Id Team");
    columnBinding.setColumnClass(wdp.entities.ready.Team.class);
    columnBinding.setEditable(false);
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${office}"));
    columnBinding.setColumnName("Office");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${loginName}"));
    columnBinding.setColumnName("Login Name");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${hired}"));
    columnBinding.setColumnName("Hired");
    columnBinding.setColumnClass(java.util.Date.class);
    columnBinding.setEditable(false);
    columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fired}"));
    columnBinding.setColumnName("Fired");
    columnBinding.setColumnClass(java.util.Date.class);
    columnBinding.setEditable(false);
    bindingGroup.addBinding(jTableBinding);
    jTableBinding.bind();
    masterTable.addMouseListener(formListener);
    masterScrollPane.setViewportView(masterTable);

    newButton.setText(resourceMap.getString("newButton.text")); // NOI18N
    newButton.setName("newButton"); // NOI18N
    newButton.addActionListener(formListener);

    javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(wdp.WdpApp.class).getContext().getActionMap(WorkerSelectFrame.class, this);
    jButtonSelect.setAction(actionMap.get("selectWorker")); // NOI18N
    jButtonSelect.setName("jButtonSelect"); // NOI18N

    jButtonNone.setText(resourceMap.getString("jButtonNone.text")); // NOI18N
    jButtonNone.setName("jButtonNone"); // NOI18N
    jButtonNone.addActionListener(formListener);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(newButton)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jButtonNone)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jButtonSelect))
          .addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jButtonSelect)
          .addComponent(newButton)
          .addComponent(jButtonNone))
        .addContainerGap())
    );

    bindingGroup.bind();
  }

  // Code for dispatching events from components to event handlers.

  private class FormListener implements java.awt.event.ActionListener, java.awt.event.MouseListener {
    FormListener() {}
    public void actionPerformed(java.awt.event.ActionEvent evt) {
      if (evt.getSource() == newButton) {
        WorkerSelectFrame.this.newButtonActionPerformed(evt);
      }
      else if (evt.getSource() == jButtonNone) {
        WorkerSelectFrame.this.jButtonNoneActionPerformed(evt);
      }
    }

    public void mouseClicked(java.awt.event.MouseEvent evt) {
      if (evt.getSource() == masterTable) {
        WorkerSelectFrame.this.masterTableMouseClicked(evt);
      }
    }

    public void mouseEntered(java.awt.event.MouseEvent evt) {
    }

    public void mouseExited(java.awt.event.MouseEvent evt) {
    }

    public void mousePressed(java.awt.event.MouseEvent evt) {
    }

    public void mouseReleased(java.awt.event.MouseEvent evt) {
    }
  }// </editor-fold>//GEN-END:initComponents

    private class TableSelectionListener implements ListSelectionListener {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getSource() == masterTable.getSelectionModel()) {
                //boolean enabled = (masterTable.getSelectedRow() != -1);
                //deleteButton.setEnabled(enabled);
            }
        }
    }

    public Worker getSelectedEntity() {
        return selectedEntity;
    }

    /**
     * TODO dodać zaznaczanie na liście wybranej encji
     */
    public void setSelectedEntity(Worker selectedEntity) {
        this.selectedEntity = selectedEntity;
    //if(selectedEntity == null)
    //masterTable.setSetSelectedRow(-1);
    //else
    }

    public static Worker selectEntity() {
        WorkerSelectFrame win = new WorkerSelectFrame(null);
        win.setLocationRelativeTo(WdpApp.getApplication().getMainFrame());
        WdpApp.getApplication().show(win);
        return win.getSelectedEntity();
    }

  private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
        wdp.entities.ready.Worker w = new wdp.entities.ready.Worker();
      entityManager.persist(w);
      list.add(w);
      int row = list.size() - 1;
      masterTable.setRowSelectionInterval(row, row);
      masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
  }//GEN-LAST:event_newButtonActionPerformed

  private void masterTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_masterTableMouseClicked
      if (evt.getClickCount() > 1) {
          setVisible(false);
      }
  }//GEN-LAST:event_masterTableMouseClicked

  private void jButtonNoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNoneActionPerformed
      selectedEntity = null;
      setVisible(false);
  }//GEN-LAST:event_jButtonNoneActionPerformed
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.persistence.EntityManager entityManager;
  private javax.swing.JButton jButtonNone;
  private javax.swing.JButton jButtonSelect;
  private java.util.List<wdp.entities.ready.Worker> list;
  private javax.swing.JScrollPane masterScrollPane;
  private javax.swing.JTable masterTable;
  private javax.swing.JButton newButton;
  private javax.persistence.Query query;
  private org.jdesktop.beansbinding.BindingGroup bindingGroup;
  // End of variables declaration//GEN-END:variables
    private Worker selectedEntity = null;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                JDialog frame = new WorkerSelectFrame(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    @Action
    public void selectWorker() {
        setVisible(false);
    }
}
